﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Informant.UI
{
	public class Controller
	{
		#region Fields

		static Controller _instance;
		Dictionary<Type, Type> _types;
		Dictionary<Type, Object> _instances;

		#endregion

		#region Properties

		/// <summary>
		/// Gets the instance.
		/// </summary>
		/// <value>The instance.</value>
		public static Controller Instance
		{
			get
			{
				if (_instance == null)
				{
					_instance = new Controller();
				}
				return _instance;
			}
		}

		#endregion

		#region Construction

		private Controller()
		{
			_types = new Dictionary<Type, Type>();
			_instances = new Dictionary<Type, Object>();
		}

		#endregion

		#region Public Methods

		/// <summary>
		/// Gets this instance.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <returns></returns>
		public T Get<T>()
		{
			T t = default(T);
			if (_types.Keys.Contains(typeof(T)))
			{
				t = (T)Activator.CreateInstance(_types[typeof(T)]);
			}
			else if (_instances.Keys.Contains(typeof(T)))
			{
				t = (T)_instances[typeof(T)];
			}
			return t;
		}
		/// <summary>
		/// Registers T as the key and U as the value.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <typeparam name="U"></typeparam>
		public void Register<T, U>()
		{
			_types.Add(typeof(T), typeof(U));
		}
		/// <summary>
		/// Registers T as the key and instance of T as the value.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="instance">The instance.</param>
		public void Register<T>(T instance)
		{
			_instances.Add(typeof(T), instance);
		}

		#endregion
	}
}
